class MapSum {
    Map<String,Integer> hashMap;
    List<String> list = new ArrayList<>();
    /** Initialize your data structure here. */
    public MapSum() {
        hashMap = new HashMap<>();
    }
    
    public void insert(String key, int val) {
        if(!list.contains(key)) {
            list.add(key);
            hashMap.put(key,val);
        } else {
            hashMap.put(key,val);
        }
    }
    
    public int sum(String prefix) {
        int num = 0;
        int length = prefix.length();
        for (String l:list) {
            if(l.length() >= length) {
                String pre = l.substring(0,length);
                if(pre.equals(prefix)) {
                    num += hashMap.get(l);
                }
            }  
        }
        return num;
    }
}

/**
 * Your MapSum object will be instantiated and called as such:
 * MapSum obj = new MapSum();
 * obj.insert(key,val);
 * int param_2 = obj.sum(prefix);
 */